package com.wholeintech.ble;

import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattService;
import android.content.Context;
import android.util.Log;

import com.wholeintech.utils.CommonUtil;
import com.wholeintech.entity.DeviceDb;

import org.xutils.DbManager;
import org.xutils.ex.DbException;

import java.util.Vector;

import static com.wholeintech.ble.BluetoothManage.*;
import static com.wholeintech.utils.CommonIntent.ACTION_DEV_DEVCHANAGE;

/**
 * Created by Lyle on 2016-04-19.
 */
public class TopBoxDev {

    private String TAG = "TopBox";

    private String name = "";

    //public static final UUID RX_SERVICE_UUID = UUID.fromString("6e400001-b5a3-f393-e0a9-e50e24dcca9e");
    //public static final UUID RX_CHAR_UUID = UUID.fromString("6e402c00-b5a3-f393-e0a9-e50e24dcca9e");
    public static final int MODE_AUTO = 5;
    public static final int MODE_HEATING = 2;
    public static final int MODE_COLD = 1;
    public static final int MODE_DEHUM = 3;
    public static final int MODE_AIRATION = 4;

    public static final int WIND_AUTO = 0;
    public static final int WIND_LOW = 1;
    public static final int WIND_MID = 2;
    public static final int WIND_HIGH = 3;

    public static final int POWER_OFF = 0;
    public static final int POWER_ON = 1;
    public static final int POWER_UNUSED = 2;

    public static final int WIND_DIR_STABLE = 0;
    public static final int WIND_DIR_SWING = 1;

    public static final int TIMER_UNUSED = 0;
    public static final int TIMER_1_HOUR = 1;
    public static final int TIMER_2_HOUR = 2;
    public static final int TIMER_3_HOUR = 3;
    public static final int TIMER_4_HOUR = 4;
    public static final int TIMER_5_HOUR = 5;

    public static final int TRUE = 1;
    public static final int FALSE = 0;

    /*temperature 16-32
     *
     *
     */
    /*heating unused
     *
     * */

    public static final int TYPE_AUX_DEVICE = 0;
    public static final int TYPE_EPSON_DEVICE = 1;
    public static final int TYPE_AIRCLEANER_DEVICE = 2;
    public static final int TYPE_MIDEA_DEVICE = 3;
    public static final int TYPE_GREE_DEVICE = 4;
    public static final int TYPE_GuanYuan_DEVICE = 5;

    public static final int TYPE_Other_DEVICE = 2001;
    public static final int TYPE_Movie_DEVICE = 2002;    //  电影
    public static final int TYPE_Cinema_DEVICE = 2003;   //  影音

    private static byte[] AUX_AIR_FMT = {(byte) 0xc3, 0x6a, (byte) 0xe0, 0x00, (byte) 0xa0, 0x00, 0x20, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00};
    private static final int HEAD = 0xc3;
    private static final int TEMPEATURE = 1;
    private static final int POWER = 9;
    private static final int TIMER = 4;
    private static final int WIND_LEVEL = 4;
    private static final int WIND_DIR = 1;
    private static final int CHECKSUM = 12;

    private static final int M_TEMPEATURE = 4;
    private static final int M_POWER = 3;
    private static final int M_TIMER = 5;
    private static final int M_WIND_LEVEL = 3;
    private static final int M_WIND_DIR = 1;

    private static byte[] MIDEA_AIR_FMT = {(byte) 0x4d, (byte) 0xb2, (byte) 0xff, 0x00, (byte) 0x00, (byte) 0xff};
    private static byte[] MIDEA_TEMP_TABLE = {0x00, 0x08, 0x0c, 0x04, 0x06, 0x0e, 0x0a, 0x02, 0x03, 0x0b, 0x09, 0x01, 0x05, 0x0d};
    private static byte[] MIDEA_WIND_TABLE = {0x02, 0x06, 0x05, 0x03, 0x00};
    private static byte[] MIDEA_POWER_OFF_TABLE = {0x4d, (byte) 0xb2, (byte) 0xde, 0x21, 0x07, (byte) 0xf8};


    private static final int GREE_MODE_HEATING = 7;
    private static final int GREE_TEMP = 1;
    private static final int GREE_MODE = 0;
    private static final int GREE_Wind = 0;
    private static final int GREE_CHECKSUM = 8;

    private static byte[] GREE_AIR_DISP_FMT = {(byte) 0x0b, (byte) 0x0b, (byte) 0x20, (byte) 0x50, (byte) 0x0a, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x02};
    private static byte[] GREE_AIR_NON_DISP_FMT = {(byte) 0x0b, (byte) 0x0b, (byte) 0x00, (byte) 0x50, (byte) 0x0a, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x02};

    private static byte[] GREE_POWER_OFF_TABLE = {(byte) 0x03, (byte) 0x0b, (byte) 0x20, (byte) 0x50, (byte) 0x0a, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x0a};
    //0x01,0x0b,0x00,0x50,0x0a,0x20,0x02,0x00,0x08
    //private static byte[] GREE_AIR_DISP_FMT = 	  {(byte)0x0b,(byte)0x0b,(byte)0x20,(byte)0x50,(byte)0x0a,(byte)0x20,(byte)0x02,(byte)0x00,(byte)0x02};
    //private static byte[] GREE_AIR_NON_DISP_FMT = {(byte)0x0b,(byte)0x0b,(byte)0x00,(byte)0x50,(byte)0x0a,(byte)0x20,(byte)0x02,(byte)0x00,(byte)0x02};

    //private static byte[] GREE_POWER_OFF_TABLE =  {(byte)0x03,(byte)0x0b,(byte)0x00,(byte)0x50,(byte)0x0a,(byte)0x20,(byte)0x02,(byte)0x00,(byte)0x0a};  

    private static byte[] GUANYUAN_FMT = {(byte) 0x00, (byte) 0x0b, (byte) 0x20, (byte) 0x50, (byte) 0x00};

    private static byte[] OTHER_FMT = 	{(byte)0x00,(byte)0x0b,(byte)0x20,(byte)0x50,(byte)0x00,(byte)0x50,(byte)0x00,0x00};

    private int type = DeviceList.TYPE_TOPBOX;

    private boolean onLineState = false;

    Vector<DevInfoItem> mDevInfoItem;

    private Context mContext;

    private TopBoxDev() {
    }

    public TopBoxDev(Context c, Vector<DevInfoItem> d) {
        mDevInfoItem = d;
        mContext = c;
    }

    public void airConditionCtl(String address, int type, int toggle, int mode, int temp, int windlevel, int winddir, int timer) {

        if (writeRXCharacteristic(address, getAirConditionData(type, toggle, temp, timer, windlevel, winddir, mode))) {
            updateAir(address, toggle, mode, temp, windlevel, winddir);
        }
    }

    public void projectorCtl(String address, int type,int Sw) {

        if (type == TYPE_EPSON_DEVICE) {
            if (writeRXCharacteristic(address, PowerOnEpson())) {

            }
        }else if(type ==TYPE_Other_DEVICE){
            if (writeRXCharacteristic(address, otherProjectProc(Sw))) {

            }
        }
    }

    // 电影控制
    public void movieCtl(String address, int type) {

        if (type == TYPE_Movie_DEVICE) {
            if (writeRXCharacteristic(address, movieProc())) {

            }
        }
    }

    public void AirCleanerCtl(String address, int type, boolean sw, boolean mode, boolean wind, boolean timer) {
        if (sw)
            writeRXCharacteristic(address, AirCleanerSwitch());
        if (mode) {
            writeRXCharacteristic(address, AirCleanerMode());
        }
        if (wind) {
            writeRXCharacteristic(address, AirCleanerWind());
        }
        if (timer) {
            writeRXCharacteristic(address, AirCleanerTimer());
        }
    }

    private byte[] getAirConditionData(int type, int powerState, int temp, int timer, int windlevel, int winddir, int mode) {//add by engineer Cai.
        switch (type) {
            case TYPE_AUX_DEVICE:
                return auxAirCdtProc(powerState, temp, timer, windlevel, winddir, mode);
            case TYPE_MIDEA_DEVICE:
                return mideaAirCdtProc(powerState, temp, timer, windlevel, winddir, mode);
            case TYPE_GREE_DEVICE:
                return greeAirCdtProc(powerState, temp, timer, windlevel, winddir, mode);
            case TYPE_GuanYuan_DEVICE:
            	return guanYuanAirCdtProc(powerState, temp, timer, windlevel, winddir, mode);
            case TYPE_Other_DEVICE:
                return otherAirCdtProc(powerState, temp, timer, windlevel, winddir, mode);
            default:
                ;
        }

        byte[] r = {0};
        return r;
    }
    //xxxx?aD??o? aa?a?a1? bb?a????? cc ???? dd?????? ee ????? ??y?Y?a16???????
    private byte[] otherAirCdtProc(int powerState, int temperature, int timer, int windlevel, int winddir, int mode){
        OTHER_FMT[0]=0x00;
        OTHER_FMT[1]=0x03;
        if (powerState == 0)
            OTHER_FMT[2]= 0;
        else
            OTHER_FMT[2]= 1;

        if (temperature <= 30 && temperature >= 16)
            OTHER_FMT[3] = (byte) temperature;
        else if (temperature > 30)
            OTHER_FMT[3] = 30;
        else if (temperature < 16)
            OTHER_FMT[3] = 16;

        OTHER_FMT[4] = (byte) mode;

        OTHER_FMT[5] = (byte) windlevel;

        OTHER_FMT[6] =  (byte) winddir;

        OTHER_FMT[7] = '*'; //������
        Log.d(TAG,"!!!!!!!!!!!!!!!!!!!!ohter！！！！！！！！！！！:" + Utils.bytesToHexString(OTHER_FMT));

        return OTHER_FMT;
    }

    private byte[] otherProjectProc(int Sw){
        OTHER_FMT[0]=0x00;
        OTHER_FMT[1]=0x02;
        if(Sw == 1){
            OTHER_FMT[2]=0x01;
        }else if(Sw == 0){
            OTHER_FMT[2]=0x00;
        }
        OTHER_FMT[3]=0x00;
        OTHER_FMT[4]=0x00;
        OTHER_FMT[5]=0x00;
        OTHER_FMT[6]=0x00;
        OTHER_FMT[7]='*';
        Log.d(TAG,"!!!!!!!!!!!!!!!!!!!!ohter！！！！！！！！！！！:" + Utils.bytesToHexString(OTHER_FMT));
        return OTHER_FMT;
    }

    private byte[] movieProc(){
        OTHER_FMT[0]=0x00;
        OTHER_FMT[1]=0x08; // 控制电视
        OTHER_FMT[2]=0x00;
        OTHER_FMT[3]=0x00;
        OTHER_FMT[4]=0x00;
        OTHER_FMT[5]=0x00;
        OTHER_FMT[6]=0x00;
        OTHER_FMT[7]='*';
        Log.d(TAG,"!!!!!!!!!!!!!!!!!!!!ohter！！！！！！！！！！！:" + Utils.bytesToHexString(OTHER_FMT));
        return OTHER_FMT;
    }


    private byte[] guanYuanAirCdtProc(int powerState, int temperature, int timer, int windlevel, int winddir, int mode) {
        if (powerState == 0)
            GUANYUAN_FMT[0] = 0;
        else
            GUANYUAN_FMT[0] = 1;

        GUANYUAN_FMT[1] = (byte) mode;

        if (temperature <= 30 && temperature >= 16)
            GUANYUAN_FMT[2] = (byte) temperature;
        else if (temperature > 30)
            GUANYUAN_FMT[2] = 30;
        else if (temperature < 16)
            GUANYUAN_FMT[2] = 16;

        GUANYUAN_FMT[3] = (byte) windlevel;


        return GUANYUAN_FMT;
    }

    public byte[] PowerOnEpson() {
        byte[] data = {1};
        return data;
    }

    public byte[] AirCleanerSwitch() {
        byte[] data = {(byte) 0xaa, 0x04, (byte) 0xbf, 0x40};
        return data;
    }

    public byte[] AirCleanerMode() {
        byte[] data = {(byte) 0xaa, 0x04, (byte) 0x7f, (byte) 0x80};
        return data;
    }

    public byte[] AirCleanerTimer() {
        byte[] data = {(byte) 0xaa, 0x04, (byte) 0xdf, (byte) 0x20};
        return data;
    }

    public byte[] AirCleanerWind() {
        byte[] data = {(byte) 0xaa, 0x04, (byte) 0xef, (byte) 0x10};
        return data;
    }

    private byte[] auxAirCdtProc(int powerState, int temperature, int timer, int windlevel, int winddir, int mode) {
        if (powerState == POWER_OFF) {
            AUX_AIR_FMT[WIND_DIR] = (byte) ((AUX_AIR_FMT[WIND_DIR] & 0xf8) | 0x07);
            AUX_AIR_FMT[POWER] = (byte) ((AUX_AIR_FMT[POWER] & 0xff) | 0x60);
            AUX_AIR_FMT[TIMER] = (byte) (0x00);
        } else {
            AUX_AIR_FMT[TEMPEATURE] = (byte) ((temperature - 0x08) << 3);
            if (timer != 0) {
                AUX_AIR_FMT[TIMER] = (byte) ((timer) & 0x0f);
                powerState = POWER_OFF;
            } else
                AUX_AIR_FMT[TIMER] = (byte) ((timer) & 0x0f);

            if (powerState == POWER_ON) {
                AUX_AIR_FMT[POWER] = 0;
                AUX_AIR_FMT[POWER] = (byte) ((AUX_AIR_FMT[POWER] & 0xff) | 0x20);
            } else if (powerState == POWER_OFF) {
                AUX_AIR_FMT[POWER] = 0;
                AUX_AIR_FMT[POWER] = (byte) ((AUX_AIR_FMT[POWER] & 0xff) | 0x60);
            } else {
                AUX_AIR_FMT[POWER] = (byte) ((AUX_AIR_FMT[POWER] & 0xff) | 0x20);
            }

            if (windlevel == WIND_AUTO)
                AUX_AIR_FMT[WIND_LEVEL] = (byte) ((AUX_AIR_FMT[WIND_LEVEL] & 0x0f) | 0xa0);
            else if (windlevel == WIND_LOW)
                AUX_AIR_FMT[WIND_LEVEL] = (byte) ((AUX_AIR_FMT[WIND_LEVEL] & 0x0f) | 0x60);
            else if (windlevel == WIND_MID)
                AUX_AIR_FMT[WIND_LEVEL] = (byte) ((AUX_AIR_FMT[WIND_LEVEL] & 0x0f) | 0x40);
            else
                AUX_AIR_FMT[WIND_LEVEL] = (byte) ((AUX_AIR_FMT[WIND_LEVEL] & 0x0f) | 0x20);

            if (winddir == WIND_DIR_STABLE)
                AUX_AIR_FMT[WIND_DIR] = (byte) ((AUX_AIR_FMT[WIND_DIR] & 0xf8) | 0x02);    //AUX_AIR_FMT[WIND_DIR]=(byte)0x20;
            else
                AUX_AIR_FMT[WIND_DIR] = (byte) ((AUX_AIR_FMT[WIND_DIR] & 0xf8) | 0x08);
        }

        AUX_AIR_FMT[CHECKSUM] = checkSUM(AUX_AIR_FMT);

        return AUX_AIR_FMT;

    }

    private byte[] mideaAirCdtProc(int powerState, int temperature, int timer, int windlevel, int winddir, int mode) {

        if (powerState == POWER_OFF) {
            //MIDEA_AIR_FMT[M_POWER]=(byte)0x21;
            //MIDEA_AIR_FMT[M_POWER-1]=(byte)0xde;
            Log.d(TAG, "mideaAirCdtProc:" + CommonUtil.bytesToHexString(MIDEA_POWER_OFF_TABLE, 0, MIDEA_POWER_OFF_TABLE.length));
            return MIDEA_POWER_OFF_TABLE;
        } else {
            if (temperature < 17)
                temperature = 17;
            if (temperature > 30)
                temperature = 30;
            MIDEA_AIR_FMT[M_TEMPEATURE] = MIDEA_TEMP_TABLE[temperature - 17];
            MIDEA_AIR_FMT[M_TEMPEATURE + 1] = (byte) ((byte) 0x00ff - (byte) MIDEA_TEMP_TABLE[temperature - 17]);
            if (windlevel > 4)
                windlevel = 4;
            if (windlevel < 0)
                windlevel = 0;
            MIDEA_AIR_FMT[M_WIND_LEVEL] = MIDEA_WIND_TABLE[windlevel];
            MIDEA_AIR_FMT[M_WIND_LEVEL - 1] = (byte) ((byte) 0x00ff - (byte) MIDEA_WIND_TABLE[windlevel]);
        }

        Log.d(TAG, "mideaAirCdtProc:" + CommonUtil.bytesToHexString(MIDEA_AIR_FMT, 0, MIDEA_AIR_FMT.length));

        return MIDEA_AIR_FMT;
    }

    private byte greeCheckSum(byte[] data) {
        byte bSum = 0;
        int sum = 0;
        int i = 0;
        for (i = 0; i < (data.length - 1); i++) {
            sum = sum + ((0x00ff) & data[i]);
        }
        bSum = (byte) (sum & 0x0f);
        return bSum;
    }

    private byte[] greeDataConv(byte[] data) {
        byte[] distData = new byte[data.length];
        int i = 0;
        for (i = 0; i < 5; i++) {
            distData[i] = data[i];
        }
        distData[5] = (byte) (((data[4] >> 4) & 0x0f) | ((data[5] << 4) & 0xf0));
        distData[6] = (byte) (((data[5] >> 4) & 0x0f) | ((data[6] << 4) & 0xf0));
        distData[7] = (byte) (((data[6] >> 4) & 0x0f) | ((data[7] << 4) & 0xf0));
        distData[8] = (byte) (((data[7] >> 4) & 0x0f) | ((data[8] << 4) & 0xf0));


        return distData;
    }

    private byte[] greeAirCdtProc(int powerState, int temperature, int timer, int windlevel, int winddir, int mode) {
        byte[] sendData;
        byte inner_mode = 0;
        byte inner_wind = 0;
        if (powerState == POWER_OFF) {
            Log.d(TAG, "greeAirCdtProc	   :" + CommonUtil.bytesToHexString(GREE_POWER_OFF_TABLE, 0, GREE_POWER_OFF_TABLE.length));
            sendData = greeDataConv(GREE_POWER_OFF_TABLE);
            Log.d(TAG, "greeAirCdtProc conv:" + CommonUtil.bytesToHexString(sendData, 0, sendData.length));
            Log.d(TAG, "checkSum:" + greeCheckSum(sendData));
            return sendData;
        } else {
            GREE_AIR_DISP_FMT[GREE_MODE_HEATING] = (byte) 0x00;
            if (temperature < 16)
                temperature = 16;
            if (temperature > 30)
                temperature = 30;
            temperature = temperature - 16;

            if (mode == MODE_AUTO) {
                inner_mode = (byte) 0x08;
            } else if (mode == MODE_HEATING) {
                inner_mode = (byte) 0x0c;
                GREE_AIR_DISP_FMT[GREE_MODE_HEATING] = (byte) 0x80;

            } else if (mode == MODE_COLD) {
                inner_mode = (byte) 0x09;
            } else if (mode == MODE_DEHUM) {
                inner_mode = (byte) 0x1a;
            } else if (mode == MODE_AIRATION) {
                inner_mode = (byte) 0x0b;
            }

            if (windlevel == WIND_AUTO) {
                inner_wind = (byte) 0x00;
            } else if (windlevel == WIND_LOW) {
                inner_wind = (byte) 0x10;
            } else if (windlevel == WIND_MID) {
                inner_wind = (byte) 0x20;
            } else {
                inner_wind = (byte) 0x30;
            }
            GREE_AIR_DISP_FMT[GREE_TEMP] = (byte) (0x0f & temperature);
            GREE_AIR_DISP_FMT[GREE_MODE] = (byte) (inner_wind | inner_mode);
            GREE_AIR_DISP_FMT[GREE_CHECKSUM] = greeCheckSum(GREE_AIR_DISP_FMT);
        }

        sendData = greeDataConv(GREE_AIR_DISP_FMT);
        Log.d(TAG, "greeAirCdtProc	   :" + CommonUtil.bytesToHexString(GREE_AIR_DISP_FMT, 0, GREE_AIR_DISP_FMT.length));
        Log.d(TAG, "greeAirCdtProc conv:" + CommonUtil.bytesToHexString(sendData, 0, sendData.length));

        //Log.d(TAG, "checkSum:"+greeCheckSum(GREE_AIR_DISP_FMT));
        return sendData;
    }

    private byte checkSUM(byte[] data) {

        int sum = 0;

        for (int i = 0; i < (data.length - 1); i++) {
            sum = sum + data[i];
        }
        return ((byte) sum);
    }

    public void setSensorData(String address, byte[] data) {
        DbManager db = CommonUtil.MyDbUtils();

        try {
//            DeviceDb me = db.findFirst(Selector.from(DeviceDb.class).where("macAddr", "=", address));
            DeviceDb me = db.selector(DeviceDb.class).where("macAddr", "=", address).findFirst();
            if (me != null) {
                me.setAmbientLight(getLightSenor(data));
                me.setPir(getPir(data));
                db.saveOrUpdate(me);

                Log.d(TAG, "Update TopBox: Brs:" + me.getAmbientLight() + ",PI:" + me.getPir());
            }

        } catch (DbException | NumberFormatException e) {
            e.printStackTrace();
        }
    }

    public int getHumidity(byte[] data) {
        int temp;
        float temp2;
        temp = (data[1] << 8) & 0xff00;
        temp = (temp | (data[0] & 0xff));
        temp2 = (float) temp / 10;
        return (int) (temp2 + 0.5);
    }

    public float getTemperature(byte[] data) {
        int temp;
        float temp2;
        temp = (data[3] << 8) & 0xff00;
        temp = (temp | (data[2] & 0xff));
        temp2 = (float) temp / 10;
        // temp2 =  temp2 / 10;
        return temp2;
    }

    public int getLightSenor(byte[] data) {
        int temp;
        temp = ((int) (data[4] & 0x00ff) + (int) (data[5] & 0x00ff) + (int) (data[6] & 0x00ff) + (int) (data[7] & 0x00ff)) / 4;
        temp = temp * 100 / 255;
        return temp;
    }

    /*Pyroelectric infrared */
    public int getPir(byte[] data) {
        int temp;
        if (data[8] == 0)
            temp = FALSE;
        else
            temp = TRUE;
        return temp;
    }

    public boolean writeRXCharacteristic(String address, byte[] value) {

        BluetoothGatt mBtGatt = null;
        int index = 0;

        for (int i = 0; i < mDevInfoItem.size(); i++) {
            if (address.equals(mDevInfoItem.get(i).getDevMAC())) {

                mBtGatt = mDevInfoItem.get(i).getBtGatt();
                index = i;
                break;
            }
        }

        if (mBtGatt == null) {
            Log.e(TAG, "writeRXCharacteristic BluetoothGatt is null," + mBtGatt);
            return false;
        }

        BluetoothGattService RxService = mBtGatt.getService(RX_SERVICE_UUID);

        if (RxService == null) {
            mBtGatt.disconnect();
            mDevInfoItem.get(index).setBtGatt(null);
            Log.e(TAG, "Rx service not found!");
            return false;
        }
        BluetoothGattCharacteristic RxChar = RxService.getCharacteristic(RX_CHAR_UUID);
        if (RxChar == null) {
            Log.e(TAG, "Rx charateristic not found!");
            return false;
        }
        RxChar.setValue(value);
        boolean status = mBtGatt.writeCharacteristic(RxChar);

        Log.i(TAG, "write TXchar - status=" + status + ",Data:" + CommonUtil.bytesToHexString(value, value.length));

        return status;
    }

    public void updateAir(String mac, int toggle, int mode, int temp, int windlevel, int winddir) {

        DbManager db = CommonUtil.MyDbUtils();

        try {
//            DeviceDb me = db.findFirst(Selector.from(DeviceDb.class).where("macAddr", "=", mac));
            DeviceDb me = db.selector(DeviceDb.class).where("macAddr", "=", mac).findFirst();
            if (me != null) {
                me.setSw(toggle);
                me.setSetTemp(temp);
                me.setMode(mode);
                me.setWind(windlevel);
                me.setWipe(winddir);
                db.saveOrUpdate(me);

                //发送状态改变广播到上层
                CommonUtil.sendBroadcastMsg(mContext,ACTION_DEV_DEVCHANAGE,me.getBoxId(),me.getDevId(),me.getType(),false,null);
                Log.e(TAG,"sendBroadcastMsg===3");
            }

        } catch (DbException e) {
            e.printStackTrace();
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
    }

    public void setName(String n) {
        name = n;
    }

    public String getName() {
        return name;
    }


    public String toString() {
        String str = "Name=" + name + ",MAC=" + "\n"
                + ",Type=" + type + ",Device State=" + onLineState + "\n";
        return str;
    }
}
